﻿namespace Dissemination.MVC.Repositories
{
    using System.Linq;
    using System.Collections.Generic;
    using Dissemination.MVC.Models;
    using System.Web.UI.WebControls;
    using log4net;

    public abstract class BaseRepository<T>
    {
        protected static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        protected DisseminationEntities dataContext = new DisseminationEntities();
        protected string EntityName;

        public abstract List<T> List();

        public abstract T Get(int id);

        public virtual void Add(T obj)
        {
            dataContext.AddObject(EntityName, obj);
        }

        public void Delete(T obj)
        {
            dataContext.DeleteObject(obj);
        }

        public void Save()
        {
            dataContext.SaveChanges();
        }

        public ListItemCollection GetOptions(int max)
        {
            var collection = new ListItemCollection();
            collection.Add(new ListItem("-", "0"));
            for (int i = 1; i <= max; i++)
            {
                string sIndex = i.ToString();
                collection.Add(new ListItem(sIndex, sIndex));
            }

            return collection;
        }

        public void AddEmptySurveyResult(Survey survey, TargetArea targetArea)
        {
            SurveyResult result = new SurveyResult();
            result.Survey = survey;
            result.TargetArea = targetArea;
            dataContext.AddObject("SurveyResults", result);
        }

    }
}
